// reducer是一个纯函数，(prevState, action) => newState
// 之所以叫reducer是因为它和Array.prototype.reduce里的回调函数属于相同类型

// 不要在reducer里面做的操作
// 1.修改传入参数
// 2.执行有副作用的操作，如API请求和跳转
// 3.调用非纯函数，如Date.now, Math.random

import {
    AddTodoType,
    RemoveTodoType,
    ShowAllType,
    VisibleFilterType
} from '../actions/actionTypes'
import { combineReducers } from 'redux'

let id = 0

const todoApp = (state=[], action) => {
    switch(action.type){
        case AddTodoType:
            return [
                ...state,
                {
                    id: id++,
                    text: action.text
                }
            ]

        case RemoveTodoType:
            return state.filter(item => item.id !== action.id)
        default:
            return state
    }
}

const visibleFilter = (state=true, action) => {
    switch(action.type) {
        case VisibleFilterType:
            return state
        case ShowAllType:
            return true
        default:
            return state
    }
}

const reducer = (state={}, action) => ({
    todos: todoApp(state.todos, action),
    filter: visibleFilter(state.filter, action)
})


const reducer2 = combineReducers({
    todos: todoApp,
    flter: visibleFilter
})

export default reducer

